{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python for Operations Research II\n", "\n", "## Try me\n", " [![Open In Colab](../../_static/colabs_badge.png)](https://colab.research.google.com/github/ffraile/operations-research-notebooks/blob/main/docs/source/Introduction/libraries/Python%20tutorial%20II.ipynb)[![Binder](../../_static/binder_badge.png)](https://mybinder.org/v2/gh/ffraile/operations-research-notebooks/main?labpath=docs%2Fsource%2FIntroduction%2Flibraries%2FPython%20tutorial%20II.ipynb)\n", "\n", "\n", "## Introduction\n", "In previous tutorials, we covered few tips to work with lists and **Numpy** Arrays in Python. This tutorial presents two python types that will be used in the following tutorials, **tuples** and **dictionaries**.\n", "\n", "### Tuples\n", "Tuples are very similar to lists, except that they are **immutable**. This means that, once you declare a tuple variable, you cannot make any changes to it (unless you are Chuck Norris).\n", "Tuples are created using parenthesis instead of brackets, like in the example below:" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "pycharm": { "is_executing": false } }, "outputs": [], "source": [ "unknowns = (\"x\", \"y\", \"z\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Since they are immutable, we can use tuples to store values that cannot change in our program. Tuples are **iterable**. We can access the values of a tuple using indexes and use them in control functions like *for* loops." ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "pycharm": { "is_executing": false } }, "outputs": [ { "name": "stdout", "text": [ "The first unknown is: x\n", "Printing unknown: x\n", "Printing unknown: y\n", "Printing unknown: z\n" ], "output_type": "stream" } ], "source": [ "# Let's print the first unknown\n", "print(\"The first unknown is: \" + unknowns[0])\n", "\n", "# Let's print all unknowns\n", "for i in unknowns:\n", " print(\"Printing unknown: \" + i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "## Dictionaries\n", "Dictionaries allow us to *map keys to values*. Note that both tuples and lists are *ordered* and that we can access the values of the elements using indexes. In dictionaries, we map a value to a key, so that we can use the key to access the value of an element, instead of its index. The syntax of dictionaries is: \n", "```python\n", "my_dict = {key1: value, key2: value2}\n", "```\n", "\n", "Then, we can access the values in the dictionary using the keys. For instance:\n" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "pycharm": { "is_executing": false } }, "outputs": [ { "name": "stdout", "text": [ "1\n" ], "output_type": "stream" } ], "source": [ "my_dict = {'x': 1, 'y':2, 'z':3}\n", "print(my_dict['x'])" ] }, { "cell_type": "markdown", "source": [ "You can access dictionary keys with the method keys(). This is useful to iterate on the keys of a dictionary:" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 36, "outputs": [ { "name": "stdout", "text": [ "key is: x , value is: 1\n", "key is: y , value is: 2\n", "key is: z , value is: 3\n" ], "output_type": "stream" } ], "source": [ "for key in my_dict.keys():\n", " print('key is: ' + key + ' , value is: ' + str(my_dict[key]))" ], "metadata": { "collapsed": false, "pycharm": { "is_executing": false } } }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Dictionary comprehension\n", "Dictionaries also support comprehension. As with list comprehension, dictionary comprehension allows us to assign keys and values very efficiently using for loops. Let us see how it works by example:" ] }, { "cell_type": "code", "execution_count": 37, "outputs": [ { "name": "stdout", "text": [ "key a has value 4\n" ], "output_type": "stream" } ], "source": [ "keys = ('a', 'b', 'c', 'd')\n", "values = (1, 2, 3, 4)\n", "\n", "new_dict = {key:value for key in keys for value in values}\n", "print('key a has value ' + str(new_dict['a']))" ], "metadata": { "collapsed": false, "pycharm": { "is_executing": false } } }, { "cell_type": "markdown", "source": [], "metadata": { "collapsed": false } } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" }, "pycharm": { "stem_cell": { "cell_type": "raw", "source": [], "metadata": { "collapsed": false } } } }, "nbformat": 4, "nbformat_minor": 2 }